<template>
	<view class="allIndex" :style="!isLogin?'max-height:calc(100vh - var(--window-top)); ':''">
		<comp-index ref="compIndex1" v-if="moduleNo == 'ydylapp1003'" :hasLeft="false" :isShare="isShare" :sharePhone="sharePhone" :shareEmail="shareEmail" :shareName="shareName" :compId="compId"></comp-index>
		<comp-index2 ref="compIndex2" v-if="moduleNo == 'ydylapp1002'" :sharePhone="sharePhone" :shareEmail="shareEmail" :shareName="shareName" :isShare="isShare" :compId="compId"></comp-index2>
		<index-all ref="compIndexAllComp" v-if="moduleNo == 'ydylapp1001'" :sharePhone="sharePhone" :version="2" :isShare="isShare" :compId="compId"></index-all>
		<user-index1 ref="userIndex1" v-if="moduleNo == 'ydylapp2003'" :isShare="isShare" :useId="useId">
		</user-index1>
		<user-index2 ref="userIndex2" v-if="moduleNo == 'ydylapp2002'" :hasLeft="false" :isShare="isShare" :useId="useId"></user-index2>
		<index-all ref="userIndexAllUser" v-if="moduleNo == 'ydylapp2001'" :version="1" :isShare="isShare" :useId="useId"></index-all>
		<my-auth-modal></my-auth-modal>
	</view>
</template>
<script>
	import Vue from 'vue';
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from "vuex";
	import {
		getToken,
		setToken
	} from '@/utils/token.js'
	// #ifdef MP-WEIXIN
	import {
		shareMixins
	} from '@/mixins/share'
	import store from '@/store/index.js'
	// #endif
	import {
		setUserOpenid
	} from '@/utils/storage.js'
	import compIndex from '@/pages/index/cardIndex/compIndex.vue'
	import compIndex2 from '@/pages/index/cardIndex/compIndex2.vue'
	import userIndex1 from '@/pages/index/cardIndex/index.vue'
	import userIndex2 from '@/pages/index/cardIndex/index2.vue'
	import indexAll from '@/pages/index/cardIndex/userIndex.vue'
	export default {
		components: {
			compIndex,
			compIndex2,
			userIndex1,
			userIndex2,
			indexAll
		},
		// #ifdef MP-WEIXIN
		mixins: [shareMixins],
		// #endif
		// 触发下拉刷新
		onPullDownRefresh() {
			this.freshPage()
			// 延迟关闭刷新动画
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		data() {
			return {
				shareData: {
					title: '名片王'
				},
				version: '',
				compId: '',
				useId: '',
				acPath: '',
				socket: null, //socket服务
				moduleNo: '',
				isShare: false,
				shareName: '',
				shareEmail: '',
				sharePhone: ''
			}
		},
		onHide: function() {
			this.$store.commit('initShare');
		},
		computed: {
			...mapGetters(['isFresh', 'isLogin', 'userInfo']),
		},
		onShow: function() {
			if (getToken('AUTH_TYPE')) {
				console.log('1111', this.$store);
				this.$store.state.user.authType = getToken('AUTH_TYPE');
				console.log('2222');
			}
			this.version = getToken('version');
			if (this.version == 2) {
				this.moduleNo = getToken('complogin')['moduleNo'] || 'ydylapp1003';
				this.compId = getToken('useNum');
				this.$store.commit('toSetShare', getToken('complogin'));
				uni.setTabBarItem({
					index: 1,
					text: '企脉'
				})
				console.log('3333');
				this.$store.dispatch('queryBrowerQty', {
					compId: this.companyId
				})
				console.log('4444');
			} else {
				this.useId = getToken('useNum');
				this.moduleNo = getToken('userInfo')['moduleNo'] || 'ydylapp2003';
				console.log('5555');
				this.$store.commit('toSetShare', getToken('userInfo'));
				console.log('6666');
				uni.setTabBarItem({
					index: 1,
					text: '人脉'
				})
				console.log('7777');
				this.$store.dispatch('queryBrowerQty', {
					userId: this.useId
				})
				console.log('8888');
			}
			if (this.moduleNo == 'ydylapp2003') {
				uni.setNavigationBarColor({
					frontColor: '#ffffff',
					backgroundColor: '#4e9ce6',
					animation: {
						duration: 400,
						timingFunc: 'easeIn'
					}
				})
			} else {
				// #ifdef H5
				uni.setNavigationBarColor({
					frontColor: '#ffffff',
					backgroundColor: '#ffffff',
					animation: {
						duration: 400,
						timingFunc: 'easeIn'
					}
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.setNavigationBarColor({
					frontColor: '#000000',
					backgroundColor: '#ffffff',
					animation: {
						duration: 400,
						timingFunc: 'easeIn'
					}
				})
				// #endif
			}
			if (this.isFresh) {
				setTimeout(() => {
					this.freshPage(getToken('useNum'), getToken('version'));
				}, 500)
				console.log('9999');
				this.$store.commit('isFresh', false);
				console.log('aaaa');
			}
			console.log('bbbb');
			this.$store.dispatch('getUnReadListByEntity');
			console.log('cccc');
		},
		onLoad: function(option) {

			console.log('option', option);
			if (option && option.wxId) { // 如果没有code，则去请求
				setUserOpenid(option.wxId);
			}
			if (option.chainTypeId) {
				setToken('chainTypeId', option.chainTypeId);
			}
			Vue.prototype.chainTypeId = option.chainTypeId;
			this.compId = getToken('complogin')['companyId']
			this.useId = getToken('userInfo')['craftsmanId']
			// #ifdef H5
			if (option.version) {
				this.version = option.version
				if (option.version == 1) {
					setToken('version', 1);
					if (getToken('userInfo') && getToken('userInfo')['craftsmanId']) {
						setToken('useNum', getToken('userInfo')['craftsmanId'])
					} else { //未登录 去登录
						this.$store.dispatch('showAuthModal');
					}
				} else if (option.version == 2) {
					if (!getToken('useNum') || !getToken('isLogin') || !getToken('token')) {
						this.$store.dispatch('showAuthModal');
					}
					if (getToken('version') == 2) {
						setToken('useNum', getToken('complogin').companyId)
						setToken('version', 2)
					} else {
						uni.navigateTo({
							url: `/pages/meSub/selectID?notPer=1&useNum=${getToken('userInfo')['craftsmanId']}`
						})
					}
				}
			}
			// #endif
			this.initPage();
		},

		methods: {
			initPage() {
				if (!this.isLogin) {
					this.$store.dispatch('showAuthModal');
					return
				}
			},
			freshPage(use = '', ver = '') {
				switch (this.moduleNo) {
					case 'ydylapp1003':
						this.$refs['compIndex1'].init()
						break;
					case 'ydylapp1002':
						this.$refs['compIndex2'].init()
						break;
					case 'ydylapp1001':
						this.$refs['compIndexAllComp'].initPage(use, ver)
						break;
					case 'ydylapp2003':
						this.$refs['userIndex1'].init()
						break;
					case 'ydylapp2002':
						this.$refs['userIndex2'].init()
						break;
					case 'ydylapp2001':
						this.$refs['userIndexAllUser'].initPage(use, ver)
						break;
				}
			}
		}
	}
</script>
<style scoped lang="scss">
</style>